

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.ui.qt.interrupt &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="pyamp.ui.qt" href="../qt.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../qt.html" accesskey="U">pyamp.ui.qt</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.ui.qt.interrupt</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The interrupt module contains the InterruptThread class which provides</span>
<span class="sd">a Thread which will periodically notify a QWidget.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">sleep</span>
<span class="kn">from</span> <span class="nn">PyQt4</span> <span class="kn">import</span> <span class="n">Qt</span><span class="p">,</span> <span class="n">QtCore</span>

<span class="kn">from</span> <span class="nn">pyamp.exceptions</span> <span class="kn">import</span> <span class="n">UndefinedFunctionError</span>


<div class="viewcode-block" id="InterruptThread"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_qt_interrupt.html#pyamp.ui.qt.interrupt.InterruptThread">[docs]</a><span class="k">class</span> <span class="nc">InterruptThread</span><span class="p">(</span><span class="n">Qt</span><span class="o">.</span><span class="n">QThread</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The InterruptThread class implements a :class:`PyQt4.Qt.QThread` to call</span>
<span class="sd">    the **interrupt** function of a given object at a specifiable rate.</span>

<span class="sd">    The InterruptThread class can define the :attr:`period` property to</span>
<span class="sd">    determine the number of seconds the :class:`PyQt4.Qt.QThread` will sleep</span>
<span class="sd">    during each iteration. The default :attr:`period` is 0.1 seconds.</span>

<span class="sd">    This class throws an :class:`amp.exceptions.UndefinedFunctionError` in the</span>
<span class="sd">    event that the **interrupt** function is not defined by the :attr:`parent`</span>
<span class="sd">    object.</span>

<span class="sd">    .. note:: The **interrupt** function takes one parameter which is the</span>
<span class="sd">              current interation number that the interrupt thread is on. The</span>
<span class="sd">              iteration number increases once per cycle.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Example():</span>
<span class="sd">            def __init__(self):</span>
<span class="sd">                self.__interrupt = InterruptThread(self, period=0.5)</span>
<span class="sd">                self.__interrupt.start()</span>

<span class="sd">            def interrupt(self, i):</span>
<span class="sd">                print i</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="n">period</span> <span class="o">=</span> <span class="mf">0.1</span>
    <span class="sd">&#39;&#39;&#39;Definine the number of seconds the :class:`PyQt4.Qt.QThread` will sleep</span>
<span class="sd">    between each cycle, prior to interrupting the :attr:`parent` object.</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * parent -- The parent of this thread.</span>
<span class="sd">        * period -- The number of seconds to sleep between each interrupt</span>

<span class="sd">        .. note:: The :attr:`parent` object must have a function named</span>
<span class="sd">                  **interrupt** defined.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Qt</span><span class="o">.</span><span class="n">QThread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="c"># Ensure that the parent has an interrupt attribute and that</span>
        <span class="c"># the attribute is a function. Throw an error if the function</span>
        <span class="c"># does not exist</span>
        <span class="n">interruptFn</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="s">&quot;interrupt&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">interruptFn</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">interruptFn</span><span class="p">,</span> <span class="s">&quot;__call__&quot;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">UndefinedFunctionError</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="s">&quot;interrupt&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__parent</span> <span class="o">=</span> <span class="n">parent</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__interruptSignal</span> <span class="o">=</span> <span class="n">QtCore</span><span class="o">.</span><span class="n">SIGNAL</span><span class="p">(</span><span class="s">&quot;interrupt&quot;</span><span class="p">)</span>

        <span class="c"># Use the given period if one is given. This value overrides the</span>
        <span class="c"># class defined property if it is specified</span>
        <span class="k">if</span> <span class="n">period</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">period</span> <span class="o">=</span> <span class="n">period</span>

        <span class="c"># Connect the interrupt signal to the parent&#39;s interrupt function</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__interruptSignal</span><span class="p">,</span> <span class="n">parent</span><span class="o">.</span><span class="n">interrupt</span><span class="p">)</span>

<div class="viewcode-block" id="InterruptThread.run"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_qt_interrupt.html#pyamp.ui.qt.interrupt.InterruptThread.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Run the interrupt thread.&#39;&#39;&#39;</span>
        <span class="c"># Keep track of the number of iterations</span>
        <span class="n">iterations</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="n">sleep</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">period</span><span class="p">)</span>

            <span class="c"># Emit the interrupt signal to notify the parent object</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__interruptSignal</span><span class="p">,</span> <span class="n">iterations</span><span class="p">)</span>
            <span class="n">iterations</span> <span class="o">+=</span> <span class="mi">1</span>

</div></div>
<div class="viewcode-block" id="Interruptable"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_qt_interrupt.html#pyamp.ui.qt.interrupt.Interruptable">[docs]</a><span class="k">class</span> <span class="nc">Interruptable</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Interruptable class provides an interface for interrupting</span>
<span class="sd">    a class at a specific interval.</span>

<span class="sd">    This class created an :class:`InterruptThread` object which notifies</span>
<span class="sd">    the :func:`interrupt` function at the specified :attr:`period`.</span>

<span class="sd">    Example::</span>

<span class="sd">        class Example(Interruptable):</span>
<span class="sd">            def __init__(self):</span>
<span class="sd">                Interruptable.__init__(self, period=1)</span>

<span class="sd">            # This function is called once per second, and i is the current</span>
<span class="sd">            # iteration number.</span>
<span class="sd">            def interrupt(self, i):</span>
<span class="sd">                print i</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * period -- The number of seconds to sleep during each cycle</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__interruptThread</span> <span class="o">=</span> <span class="n">InterruptThread</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">period</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__interruptThread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

<div class="viewcode-block" id="Interruptable.interrupt"><a class="viewcode-back" href="../../../../_generated/pyamp_ui_qt_interrupt.html#pyamp.ui.qt.interrupt.Interruptable.interrupt">[docs]</a>    <span class="k">def</span> <span class="nf">interrupt</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_i</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Override the interrupt function. This function is called once</span>
<span class="sd">        per cycle of the :class:`InterruptThread`.</span>

<span class="sd">        .. note:: This function should be overridden by subclasses.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">pass</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../qt.html" >pyamp.ui.qt</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>